package com.example.onlinetaxi.ui.activity.SearchPage;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AutoCompleteTextView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import androidx.appcompat.widget.Toolbar;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.sug.OnGetSuggestionResultListener;
import com.baidu.mapapi.search.sug.SuggestionResult;
import com.baidu.mapapi.search.sug.SuggestionSearch;
import com.baidu.mapapi.search.sug.SuggestionSearchOption;
import com.example.onlinetaxi.BaiDuMap.BaiDuMapManager;
import com.example.onlinetaxi.LocalStroage.LocalStorage;
import com.example.onlinetaxi.R;
import com.example.onlinetaxi.ui.activity.BaseActivity;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class StartSearchActivity extends BaseActivity {

    private ListView mSugListView;
    private SuggestionSearch mSuggestionSearch;
    // 获取结果集
    List<HashMap<String, String>> suggest;

    LatLng startLatLng;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start_search);
        // 初始化搜索框
        setStartSearchToolBar();

        // 搭车人城市信息需要放在数据库！！
        setEdit(LocalStorage.city);

        initView();

        initEvent();
    }

    private void initEvent() {
        mSugListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 根据id确定用户点击位置的信息
                HashMap<String, String> clickedItem = suggest.get((int) id);
                // 获取经纬度信息
                Gson gson = new Gson();
                startLatLng = gson.fromJson(clickedItem.get("pt").toString(), LatLng.class);

                // 设置地图中心点为所选起点
                BaiDuMapManager.baiDuMapManager.setCenter(startLatLng);

                // 返回MainActivity
                StartSearchActivity.this.onBackPressed();
            }
        });
    }

    public LatLng getStartPoint() {
        return startLatLng;
    }

    private void initView() {
        // 获取search实例
        mSuggestionSearch = SuggestionSearch.newInstance();
        // 设置监听器
        mSuggestionSearch.setOnGetSuggestionResultListener(listener);

        // 推荐列表
        mSugListView = findViewById(R.id.sug_list);
    }

    // 监听输入框
    protected void setEdit(final String city) {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_search);

        AutoCompleteTextView mEditAddress = toolbar.findViewById(R.id.editaddress);

        // 为AutoCompleteTextView添加文本改变事件
        mEditAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mEditAddress.showDropDown();
            }
        });

        mEditAddress.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                // 输入框内容改变时，改变搜索范围
                mSuggestionSearch.requestSuggestion((new SuggestionSearchOption())
                        .keyword(mEditAddress.getText().toString())
                        .city(city));
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    // 查询监听器
    private OnGetSuggestionResultListener listener = new OnGetSuggestionResultListener() {
        public void onGetSuggestionResult(SuggestionResult res) {
            if (res == null || res.getAllSuggestions() == null) {
                // 未找到相关结果
                return;
            } else {
                // 获取在线建议检索结果
                suggest = new ArrayList<>();
                for (SuggestionResult.SuggestionInfo info : res.getAllSuggestions()) {
                    if (info.getKey() != null && info.getDistrict() != null && info.getCity() != null) {
                        HashMap<String, String> map = new HashMap<>();
                        map.put("key", info.getKey());
                        map.put("description", info.getCity() + info.getDistrict() + info.getAddress());
                        map.put("city", info.getCity());
                        LatLng latLng = info.getPt();
                        Gson gson = new Gson();
                        // 放入经纬度信息
                        map.put("pt", gson.toJson(latLng));
                        suggest.add(map);
                    }
                }


                SimpleAdapter simpleAdapter = new SimpleAdapter(
                        getApplicationContext(),
                        suggest,
                        R.layout.item,
                        new String[]{"key", "description"},
                        new int[]{R.id.name, R.id.address}
                );

                mSugListView.setAdapter(simpleAdapter);
                simpleAdapter.notifyDataSetChanged();
            }
        }
    };
}